package com.example.ross.handler;

import com.example.ross.common.Result;
import com.example.ross.utils.IpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * @version 1.0.0
 * @className: GlobalException
 * @description: 全局异常处理
 * @author: zhangjunfa
 * @date: 2023/5/7 9:32
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 系统异常
     *
     * @param e
     * @param request
     * @return
     */
    @ExceptionHandler(Exception.class)
    public Result<?> handlerException(Exception e, HttpServletRequest request) {
        String message = e.getMessage();
        String uri = request.getRequestURI();
        log.error("请求地址'{}',IP：{},发生系统异常:{}", uri, IpUtils.getIpAddr(request), e.getMessage());
        return Result.error(message);
    }
}
